﻿
namespace Ordering.Domain.Models
{
    public class Order:Aggregate<OrderId>
    {
        private readonly List<OrderItem> orderItems = new();
        public IReadOnlyList<OrderItem> OrderItems => orderItems.AsReadOnly();
        public CustomerId CustomerId { get; private set; }=default!;
        public OrderName OrderName { get; private set; }=default!;
        public Address ShippingAddress { get; private set; }=default!;
        public Address BillingAddress { get; private set; }=default!;
        public Payment Payment { get; private set; }=default!;
        public OrderStatus Status { get; private set; }=OrderStatus.Pending;

        public decimal TotalPrice
        {
            get=> orderItems.Sum(x => x.Price * x.Quantity);
            private set { }
        }

        public static Order Create(OrderId orderId, CustomerId customerId, OrderName orderName, Address shippingAddress, Address billingAddress, Payment payment)
        {
            var order = new Order();
            order.Id = orderId;
            order.CustomerId = customerId;
            order.OrderName = orderName;
            order.ShippingAddress = shippingAddress;
            order.BillingAddress = billingAddress;
            order.Payment = payment;
            order.Status = OrderStatus.Pending;

            order.AddDomainEvent(new OrderCreatedEvent(order));

            return order;
        }

        public void Update(OrderName orderName, Address shippingAddress, Address billingAddress, Payment payment, OrderStatus status)
        {
            OrderName = orderName;
            ShippingAddress = shippingAddress;
            BillingAddress = billingAddress;
            Payment = payment;
            Status = status;

           AddDomainEvent(new OrderUpdatedEvent(this));
        }

        public void Add(ProductId productId, int quantity, decimal price)
        {
            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(quantity);
            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(price);

            var orderItem = new OrderItem(Id, productId, quantity, price);
            orderItems.Add(orderItem);
        }

        public void Remove(ProductId productId)
        {
            var orderItem = orderItems.FirstOrDefault(x => x.ProductId == productId);
            if (orderItem is not null)
            {
                orderItems.Remove(orderItem);
            }
        }
    }
}
